package com.tuoren.forward.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.tuoren.forward.constant.CommonConstant;
import com.tuoren.forward.entity.User;

import cn.dev33.satoken.stp.StpUtil;

public class AccessTokenUtil {
	
	public static String getAccessToken() {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		String token = request.getHeader(CommonConstant.TOKEN_NAME);
		return token;
	}
	
	public static User getUser() {
//		@SuppressWarnings("unchecked")
//		RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) SpringUtil.getBean("redisTemplate");
//		User user = (User) redisTemplate.opsForValue().get(getAccessToken());
		if(StpUtil.isLogin()) {
			return (User)StpUtil.getSession().get("user");
		}
		return null;
	}
	
	public static String getUserId() {
		return (String) StpUtil.getLoginId();
	}
}
